package com.zhuss.food.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.order.wechat.CreateOrderBo;
import com.zhuss.bean.bo.food.order.wechat.CreateOrderProductBo;
import com.zhuss.bean.bo.food.order.wechat.CreateTakeoutOrderBo;
import com.zhuss.bean.unit.core.user.UserBean;
import com.zhuss.bean.vo.food.product.DetailProductVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.EnumConfig;
import com.zhuss.core.processor.UserBindCheck;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.ConfigTypeEnum;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.enums.ProductType;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.ProductDao;
import com.zhuss.util.JsonUtil;


@Component("takeoutOrderCreate")
public class TakeoutOrderCreate extends AbstractProcessor<Object, CreateTakeoutOrderBo> implements Processor<Object, CreateTakeoutOrderBo> {
	
	@Autowired
	@Qualifier("orderCreate")
	private Processor<Object, CreateOrderBo> orderCreate;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
	
	@Autowired
	@Qualifier("enumConfig")
	private EnumConfig enumConfig;
	
	@Autowired
	private ProductCheck productCheck;
	
	@Autowired
	private UserBindCheck userBindCheck;
	
	@Override
	public void check(CreateTakeoutOrderBo bo) throws WpsException {
		Long userId = bo.getConcurrentUserId();
		Long productId = bo.getProductId(); // 购买的商品
		
		DetailProductVo product = (DetailProductVo) productRead.detail(productId);
		Integer area = bo.getArea();
		
		if (!userBindCheck.process(userId)) {
			throw new WpsException(ExceptionCode.用户信息过期);
		}
		
		Date start = enumConfig.getTime(ConfigTypeEnum.外卖开抢时间.value(), area);
		Date end = enumConfig.getTime(ConfigTypeEnum.外卖截止时间.value(), area);
		long now = System.currentTimeMillis();
		if (start.getTime() < now && now < end.getTime()) {
			
		} else {
			throw new WpsException(ExceptionCode.超过外卖打包时间);
		}
		
		if (!productCheck.checkCreateOrder(productId)) {
			throw new WpsException(ExceptionCode.当前商品已下架);
		}
		
		UserBean user = bo.getConcurrentUser();
		Integer isLimitArea = user.getIsLimitArea();
		
		Integer productArea = product.getArea();
		if (!productArea.equals(area)) {
			throw new WpsException(ExceptionCode.下单区域商品不匹配);
		}
		
		Integer userArea = bo.getConcurrentUser().getArea();
		if (!userArea.equals(area) && IsEnum.是.sameKey(isLimitArea)) {
			throw new WpsException(ExceptionCode.请在所属区域下单);
		}
		
	}

	@Override
	public Object process(CreateTakeoutOrderBo bo) throws WpsException {
		
		Long userId = bo.getConcurrentUserId();
		Long count = bo.getCount(); // 订购的总件数
		Long price = bo.getPrice(); // 订购的总件数的总金额
		Long productId = bo.getProductId(); // 购买的商品
		
		DetailProductVo product = (DetailProductVo) productRead.detail(productId);
		
		// 扣减商品库存 ，  返回受影响数量
		Integer influnce = productDao.used(productId, count);
		if (influnce.equals(0)) {
			throw new WpsException(ExceptionCode.特价外卖库存不足);
		}
		
		List<CreateOrderProductBo> createOrderProductBoList = new ArrayList<CreateOrderProductBo>();
			
		Long amount = price * count; // 总价
			
		CreateOrderProductBo c = new CreateOrderProductBo();
		c.setAmount(amount);
		c.setCount(count.longValue());
		c.setPrice(price);
		c.setProductName(product.getName());
		c.setProductNo(product.getProductNo());
		c.setProductId(productId);
		c.setStatus(OrderStatus.未取餐.key());
		c.setConcurrentUserId(userId);
		createOrderProductBoList.add(c);
		
		CreateOrderBo orderBo = new CreateOrderBo();
		BeanUtils.copyProperties(bo, orderBo);
		orderBo.setAmount(amount);
		orderBo.setCount(count.longValue());
		orderBo.setProductCount(1l);
		orderBo.setStatus(OrderStatus.未取餐.key());
		orderBo.setType(ProductType.特价外卖.key());
		orderBo.setConcurrentUserId(userId);
		orderBo.setCreateOrderProductBoList(createOrderProductBoList);
		orderBo.setArea(bo.getConcurrentArea());
		orderCreate.process(orderBo);
		bo.setTargetId(orderBo.getTargetId());
		return null;
	}

	@Override
	public Object log(CreateTakeoutOrderBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.外卖打包订单创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(EnumUtil.containsKey(ProductType.class, ProductType.特价外卖.key()));
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}

}
